/*====================================================================================
                                                                     
		          O-Ring Production Networks
			        By Demir-Fieler-Xu-Yang
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here


/***************************************
/Figure 1
****************************************/

use manufsample_2015.dta,clear


 foreach y of varlist supavgwage_w avgwage_adj_b {	 
 	bys nace4_b province_code_b, sort: egen avg_`y'=mean(`y')
	replace `y'=`y'-avg_`y'
 }

tw lpoly supavgwage_w avgwage_adj_b ,legend(off) graphregion(color(white)) ytitle("Log of supplier's wage (average)") xtitle("Log of buyer's wage")												


/*************************************************
/Matrix used in Figure 2 and (partly) Figure 4
**************************************************/

use temp2,clear

keep if year==2015

keep if nace2 <34 // Keep manufacturing firms
drop if avgwage_adj==0 | avgwage_adj==.

sort avgwage_adj
xtile wageq = avgwage_adj, nq(5) 
collapse (mean) wageq,by(ID)

sort ID
save temp3.dta,replace

//Purchases
use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

gen ID=seller_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
drop _merge ID
rename wageq wageq_s

gen ID=buyer_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
drop _merge ID
rename wageq wageq_b

drop if wageq_b==. | wageq_s==.

bys buyer_ID wageq_s,sort gen indegree=_N

collapse (sum) value indegree,by(wageq_b wageq_s)

bys wageq_b,sort: egen total_value=sum(value)
gen fraction_purch=value/total_value
drop value total_value

bys wageq_b,sort: egen total_indegree=sum(indegree)
gen fraction_suppliers=indegree/total_indegree
drop indegree total_indegree

reshape wide fraction_purch fraction_suppliers,i(wageq_b) j(wageq_s)

//Sales
use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

gen ID=seller_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
drop _merge ID
rename wageq wageq_s

gen ID=buyer_ID
destring ID,replace 
sort ID
merge ID using temp3.dta
drop _merge ID
rename wageq wageq_b
capture erase temp3.dta

drop if wageq_b==. | wageq_s==.

bys seller_ID wageq_b,sort gen outdegree=_N

collapse (sum) value outdegree,by(wageq_b wageq_s)

bys wageq_s,sort: egen total_value=sum(value)
gen fraction_sales=value/total_value
drop value total_value

bys wageq_s,sort: egen total_outdegree=sum(outdegree)
gen fraction_buyers=outdegree/total_outdegree
drop outdegree total_outdegree

reshape wide fraction_sales fraction_buyers,i(wageq_s) j(wageq_b)


/*************************************************
/Figure A1
**************************************************/

use wfe2.dta,clear

keep if year==2015

sort ID
merge ID using manufsample_2015.dta
keep if _m==3
drop _m

foreach y of varlist w_fe avgwage_adj_b {
 	bys nace4_b province_code_b, sort: egen avg_`y'=mean(`y')
	replace `y'=`y'-avg_`y'
}

su w_fe,det
keep if w_fe>r(p1) & w_fe<r(p99)

tw lpolyci avgwage_adj_b w_fe,graphregion(color(white)) xtitle("Average worker type") ytitle("Average wages") legend(off) 

/*************************************************
/Figure A2
**************************************************/

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

//Merge with average worker fixed effects
gen  ID=buyer_ID
destring ID,replace
sort ID year
merge ID year using "wfe2.dta"
keep if _merge==3 
drop _merge ID  f_fe
rename w_fe w_fe_b

gen  ID=seller_ID
destring ID,replace
sort ID year
merge ID year using "wfe2.dta"
keep if _merge==3 
drop _merge ID  f_fe
rename w_fe w_fe_s

gen supavgskill_w=w_fe_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgskill_uw=mean(w_fe_s)
gen supavgskill_cov=(w_fe_s-supavgskill_uw)*(value_share-1/indegree)

collapse (mean) w_fe_b supavgskill_uw avgwage_adj_b (sum) supavgskill_w supavgskill_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgskill_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

foreach y of varlist w_fe_b supavgskill_w {
 	bys nace4_b province_code_b, sort: egen avg_`y'=mean(`y')
	replace `y'=`y'-avg_`y'
}


tw lpolyci supavgskill_w w_fe_b  ,graphregion(color(white))  xtitle("Buyer skill")  ytitle( "Average supplier skills") legend(off)

/***************************************
/Figure A3
****************************************/
//Identify exporters

use "ihracat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet exports

keep if year==2015

collapse (sum) exports,by(ID)

destring ID,replace
sort ID
save temp3.dta,replace

use manufsample_2015.dta,clear

gen  ID=buyer_ID
destring ID,replace 

sort ID
merge ID using temp3.dta
drop if _m==2
drop _m
capture erase temp3.dta

gen exporter=(exports!=.)

local varlist="supavgwage_w avgwage_adj_b"

 foreach y of local varlist  {	 
 	bys nace4_b province_code_b, sort: egen avg_`y'=mean(`y')
	replace `y'=`y'-avg_`y'
 }


tw (lpoly supavgwage_w avgwage_adj_b if  exporter==0,lcolor(black))  (lpoly supavgwage_w avgwage_adj_b if exporter==1, lcolor(black) lpattern(dash)),legend(on order(1 2) label(1 "Non-exporters") label(2 "Exporters")) graphregion(color(white)) ytitle("Average supplier wage") xtitle("Buyer wage")		


/***************************************
/Figure A4
****************************************/

use manufsample_2015.dta,clear

gen sector=""
for X in num 10/12: replace sector="Food, beverages, and tobacco"	 if nace2==X
for X in num 13/18 \ Y in any "Textiles" "Apparel" "Leather products" "Wood and wood products" "Paper products" "Printing": replace sector="Y"	 if nace2==X
for X in num 19/21: replace sector="Petroleum and chemicals"	 if nace2==X
for X in num 22/25 \ Y in any "Rubber and plastic products" "Other non-metallic products" "Basic metals" "Fabricated metal products": replace sector="Y"	 if nace2==X
for X in num 26/27: replace sector="Electronics and electrical equip."	 if nace2==X
for X in num 28 29 31 32 33 \ Y in any "Machinery and equipment nec" "Motor vehicles" "Furniture" "Other manufacuring" "Repair of mach. and equip.": replace sector="Y"	 if nace2==X

drop if sector==""

egen sr=group(nace4_b province_code_b)

gen beta_w=.
gen beta_uw=.
gen beta_cov=.

levelsof sector, local(sector_str)
foreach s of local sector_str {
reghdfe supavgwage_w avgwage_adj_b  if sector=="`s'",absorb(sr) vce(robust)
replace beta_w=_b[avgwage_adj_b] if sector=="`s'"

reghdfe supavgwage_uw avgwage_adj_b   if sector=="`s'",absorb(sr) vce(robust)
replace beta_uw=_b[avgwage_adj_b] if sector=="`s'"

reghdfe supavgwage_cov avgwage_adj_b   if sector=="`s'",absorb(sr) vce(robust)
replace beta_cov=_b[avgwage_adj_b] if sector=="`s'"
     }

collapse (mean) beta_* ,by(sector)

graph hbar beta_uw beta_cov, over(sector,sort(beta_w) descending)  stack bar(1, color(gs6)) bar(2, color(black)) graphregion(color(white)) legend(on order(1 2) label(1 "Extensive margin") label(2 "Intensive margin"))


/***************************************
/Figure A5 and Table A6
****************************************/

use temp2.dta,clear

keep if year==2015

 foreach y of varlist avgwage sales {	 
 	bys nace4, sort: egen avg_`y'=mean(`y')
	gen indadj_`y'=ln(`y'/avg_`y')
 }
 
keep ID indadj_*
destring ID,replace
sort ID
save temp3.dta,replace

use manufsample_2015.dta,clear

gen  ID=buyer_ID
destring ID,replace
sort ID 
merge ID year using "temp3.dta"
keep if _merge==3 
drop _merge ID  
for X in any indadj_sales indadj_avgwage: rename X X_b

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID year using "temp3.dta"
keep if _merge==3 
drop _merge ID  
for X in any indadj_sales indadj_avgwage: rename X X_s
capture erase temp3.dta

 
canon ( indadj_avgwage_b indadj_sales_b ) ( indadj_avgwage_s indadj_sales_s ),stdcoef
estat correlations

 predict u1, u corr(1)
 predict v1, v corr(1)
 
 xtile bin=u1,n(100)
 collapse (mean) u1 v1,by(bin)
 tw scatter v1 u1,graphregion(color(white)) legend(off) msymbol(circle_hollow) mcolor(black)  xtitle("Buyer's attractiveness (A{subscript:b})") ytitle("Supplier's attractiveness (A{subscript:s})")
 
 
/***************************************
/Figure A6
****************************************/

//Construct annual imports

use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

keep if year==2015

collapse (sum) imports,by(ID)

destring ID,replace
sort ID
save temp3.dta,replace

//Identify buyers in the main sample

use manufsample_2015.dta,clear
keep ID
sort ID
save temp4.dta,replace

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

destring ID,replace
sort ID
merge ID using temp4.dta
keep if _m==3 // Keep buyers in the main sample
drop _m
capture erase temp4.dta

gen value_network=value if nace2_s<34

collapse (sum) value value_network (mean) wagebill_b workers_b,by(buyer_ID nace4_b nace2_b  province_code_b )
rename buyer_ID ID

destring ID,replace
sort ID
merge ID using temp3.dta
drop if _m==2
drop _m
capture erase temp3.dta

replace imports=0 if imports==.  // imports are in USD, so will be converted to TRY below using the average annual exchange rate in 2015

gen material_input_share=(value_network+2.7*imports)/(value+2.7*imports+wagebill_b)
gen matserv_input_share=(value+2.7*imports)/(value+2.7*imports+wagebill_b)

gen lnsize=ln(workers_b)

su workers_b,det
keep if workers_b>r(p1) & workers_b<r(p99)

tw (lpolyci material_input_share lnsize ,lcolor(black)) (lpolyci matserv_input_share lnsize, lcolor(black) lpattern(dash)),ylabel(0(0.2)1) xtitle("Size (in logs)") ytitle("Cost share") legend(on order(  2 4)  label( 2 "Manufacturing inputs")  label(4 "Manufacturing and service inputs") row(2)) graphregion(color(white))


